package com.example.administrator.androidapp;

import android.app.WallpaperManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

public class Ch4Activity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_ch4_2);

        //1）获取普通界面组件；
        ImageView imageView=(ImageView)findViewById(R.id.ch4_2_iv);
        //2）实现事件监听类，该监听类是一个特殊的java类，必须实现一个XXXListener接口；
        //3）调用事件源的setXXXListener方法注册事件监听器。
        imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                //当长按事件发生时，执行
                WallpaperManager wallpaperManager=(WallpaperManager)getSystemService(WALLPAPER_SERVICE);
                try {
                    wallpaperManager.setResource(R.raw.img);
                }catch (Exception e){
                    Log.e(Ch4Activity2.class.toString(),e.toString());
                }

                return true;//返回true,代表处理完毕
            }
        });

    }


}
